package com.xtkj.superclub_friendscircle.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目名称：superclub
 * 类 名 称：HelloSender
 * 类 描 述：TODO
 * 创建时间：2020/1/5 23:24
 * 创 建 人：xiezhiyan
 */
@RestController
@RequestMapping("/rabbitmq")
public class HelloSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;//使用RabbitTemplate,这提供了接收/发送等等方法

    @RequestMapping(value = "/sendDirectMessage",method = RequestMethod.GET)
    public void send() {
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
    }
}